const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: "", //用户信息
    usedPoints: 0, //累计消耗积分
    userScore: 0, //用户积分数
    diamondNum: 0, //钻石数
    userBalance: 0, //账户余额
    actions: [{
        name: '提现额度',
      },
      {
        name: '钻石',
      },
      {
        name: '报销额度',
      },
      {
        name: '报销总额',
      },
    ],
    show: false,
    selectNav: '提现额度',
    isShow: false,

    showNum: 5, //展示数
    list: [],
    showList: [],


  },


  showAllHandle() {
    var isShow = this.data.isShow,
      showNum = this.data.showNum;

    isShow = !isShow
    showNum = isShow ? this.data.showList.length : 5

    this.setData({
      isShow,
      showNum
    })




  },
  onSelect(e) {
    console.log(e);

    this.setData({
      selectNav: e.detail.name
    })
    this.dataHandle()
  },
  showHandle() {
    this.setData({
      show: true
    })
  },
  onClose() {
    this.setData({
      show: false
    })
  },


  toPage(e) {
    var flag = e.currentTarget.dataset.flag;
    console.log(flag);
    if (flag == 'diamond') {
      wx.navigateTo({
        url: '/mainPackage/pages/diamondMall/diamondMall',
      })
    } else if (flag == 'yue') {

      var arr = [],
        list = this.data.list;
      list.forEach(item => {
        if (item.usedMoney > 0) {
          arr.push({
            orderNum: item.orderNumber,
            date: item.createTime,
            scoreType: 0,
            num: item.usedMoney
          })
        }
        if (item.acceptMoney > 0) {
          arr.push({
            orderNum: item.orderNumber,
            date: item.createTime,
            scoreType: 1,
            num: item.acceptMoney
          })
        }
      })
      console.log(arr);
      // return

      wx.navigateTo({
        url: '/userPackage/pages/balanceList/balanceList?list=' + JSON.stringify(arr),
      })
    } else if (flag == 'orderDetail') {

      var obj = e.currentTarget.dataset.obj;
      console.log(obj);
      if (obj.orderNum == 0) {
        return
      }
      this.getDetail(obj.orderNum)
    }
  },

  getDetail(orderNum) {
    var url = app.globalData.url + "ScOrder/listOrderByNumber";
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        orderNum,
      }, res => {
        console.log(res);
        if (res.flag) {
          var datas = res.data;
          console.log(datas);
          wx.hideLoading()
          wx.navigateTo({
            url: '/userPackage/pages/orderDetail/orderDetail?order=' + JSON.stringify(datas),
          })

        }
      },
      err => {
        console.log(err);
      })
  },


  getData() {
    var url = app.globalData.url + "scUser/listMyAccount",
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        userId: app.globalData.userId,
      }, res => {
        console.log(res);
        if (res.flag) {
          var datas = res.data;
          console.log(datas);

          that.setData({
            list: datas.userScores
          })
          wx.hideLoading()

          that.dataHandle()

        }
      },
      err => {
        console.log(err);
      })


  },

  dataHandle() {
    var list = this.data.list,
      selectNav = this.data.selectNav,
      showList = [];

    console.log(list);

    if (selectNav == '提现额度') {
      list.forEach(item => {
        if (item.usedMoney > 0) {
          showList.push({
            orderNum: item.orderNumber,
            date: item.createTime,
            scoreType: 0,
            num: item.usedMoney
          })
        }
        if (item.acceptMoney > 0) {
          showList.push({
            orderNum: item.orderNumber,
            date: item.createTime,
            scoreType: 1,
            num: item.acceptMoney
          })
        }
      })
    } else if (selectNav == '报销额度') {
      list.forEach(item => {
        if (item.scoreNum > 0) {
          showList.push({
            orderNum: item.orderNumber,
            date: item.createTime,
            scoreType: item.scoreType,
            num: item.scoreNum
          })
        }
      })
    } else if (selectNav == '钻石') {
      list.forEach(item => {
        if (item.diamondNum > 0) {
          showList.push({
            orderNum: item.orderNumber,
            date: item.createTime,
            scoreType: item.scoreType,
            num: item.diamondNum
          })
        }
      })
    } else if (selectNav == '报销总额') {
      list.forEach(item => {
        if (item.scoreNum > 0 && item.scoreType == 0) {
          showList.push({
            orderNum: item.orderNumber,
            date: item.createTime,
            scoreType: item.scoreType,
            num: item.scoreNum
          })
        }
      })

    }
    this.setData({
      showList
    })
    console.log(showList);


  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      user: JSON.parse(wx.getStorageSync('user')),
      selectNav: options.nav || '提现额度'
    })
    console.log(this.data);

    this.getData()


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userScore: app.globalData.userScore,
      userBalance: app.globalData.userBalance,
      diamondNum: app.globalData.diamondNum,
      usedPoints: app.globalData.usedPoints,
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})